package mylittleinvestigations.CaseInformation;

import java.io.IOException;
import java.util.HashMap;
import mylittleinvestigations.DialogCharacter;
import mylittleinvestigations.EasingFunctions.*;
import mylittleinvestigations.State;
import mylittleinvestigations.Utils;
import org.newdawn.slick.*;

public class DialogCharacterManager
{
    private HashMap<String, DialogCharacter> characterByIdHashMap;

    public DialogCharacterManager()
    {
        this.characterByIdHashMap = new HashMap<String, DialogCharacter>();
    }
    
    public void UpdateForState(State state, GameContainer gc, int delta)
    {
        this.Update(state.getLeftCharacterId(), state.getLeftCharacterEmotionId(), gc, delta);
        this.Update(state.getLeftReplacementCharacterId(), state.getLeftReplacementCharacterEmotionId(), gc, delta);
        this.Update(state.getRightCharacterId(), state.getRightCharacterEmotionId(), gc, delta);
        this.Update(state.getRightReplacementCharacterId(), state.getRightReplacementCharacterEmotionId(), gc, delta);
    }

    public void DrawForState(State state, GameContainer gc, Graphics g)
    {
        this.Draw(state.getLeftCharacterId(), state.getLeftCharacterEmotionId(), state.getIsLeftCharacterTalking(), state.getShouldLeftCharacterChangeMouth(), false /* isRightSide */, state.getLeftCharacterXOffsetEasingFunction(), gc, g);
        this.Draw(state.getLeftReplacementCharacterId(), state.getLeftReplacementCharacterEmotionId(), false /* isTalking */, false /* shouldChangeMouth */, false /* isRightSide */, state.getLeftReplacementCharacterXOffsetEasingFunction(), gc, g);
        this.Draw(state.getRightCharacterId(), state.getRightCharacterEmotionId(), state.getIsRightCharacterTalking(), state.getShouldRightCharacterChangeMouth(), true /* isRightSide */, state.getRightCharacterXOffsetEasingFunction(), gc, g);
        this.Draw(state.getRightReplacementCharacterId(), state.getRightReplacementCharacterEmotionId(), false /* isTalking */, false /* shouldChangeMouth */, true /* isRightSide */, state.getRightReplacementCharacterXOffsetEasingFunction(), gc, g);
    }
    
    public void Update(String characterId, String emotionId, GameContainer gc, int delta)
    {
        if (Utils.isNullOrWhiteSpace(characterId))
        {
            return;
        }

        DialogCharacter characterToUpdate = characterByIdHashMap.get(characterId);
        characterToUpdate.Update(gc, delta, emotionId);
    }

    public void Draw(String characterId, String emotionId, boolean isTalking, boolean shouldChangeMouth, boolean isRightSide, EasingFunction characterXOffsetEasingFunction, GameContainer gc, Graphics g)
    {
        int xOffset = (int)(characterXOffsetEasingFunction == null ? 0 : characterXOffsetEasingFunction.GetCurrentValue());

        if (Utils.isNullOrWhiteSpace(characterId))
        {
            return;
        }

        DialogCharacter characterToDraw = characterByIdHashMap.get(characterId);
        characterToDraw.Draw(
            gc,
            g,
            emotionId,
            isTalking,
            shouldChangeMouth,
            isRightSide,
            xOffset);
    }

    public void AddCharacter(String characterId, DialogCharacter character)
    {
        this.characterByIdHashMap.put(characterId, character);
    }

    public String GetCharacterNameFromId(String characterId)
    {
        DialogCharacter character = this.characterByIdHashMap.get(characterId);
        return character != null ? character.getName() : "";
    }

    public DialogCharacter GetCharacterFromId(String characterId)
    {
        return this.characterByIdHashMap.get(characterId);
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("DialogCharacterManager");
        
        writer.StartElement("CharacterByIdHashMap");
        String[] ids = this.characterByIdHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            this.characterByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.EndElement();
    }

    DialogCharacterManager(CaseXmlReader reader)
    {
        this.characterByIdHashMap = new HashMap<String, DialogCharacter>();
        
        reader.StartElement("DialogCharacterManager");
        
        reader.StartElement("CharacterByIdHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            DialogCharacter character = new DialogCharacter(reader);
            this.characterByIdHashMap.put(id, character);
        }
        
        reader.EndElement();
        
        reader.EndElement();
    }

    void markPartnerChildSprites()
    {
        /*String[] ids = this.characterByIdHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            this.GetCharacterFromId(ids[i]).markChildSprites(ids[i]);
        }*/
    }
}